package com.xuecheng.test.rabbitmq;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

//Topics
public class Producer04_topics {
    //创建队列名称
    private static final  String QUEUE_INFORM_EMAIL="queue_inform_email";
    private static final  String QUEUE_INFORM_SMS="queue_inform_sms";
    //创建交换机
    private static final String EXCHANGE_TOPICS_INFORM="exchange_topics_inform";

    public static void main(String[] args) {
        ConnectionFactory connectionFactory=null;
        Connection connection=null;
        Channel channel=null;
        try {
        //创建连接
        connectionFactory = new ConnectionFactory();
        //连接mq
        connectionFactory.setHost("localhost");
        //设置mq端口
        connectionFactory.setPort(5672);
        //设置用户密码
        connectionFactory.setUsername("guest");
        connectionFactory.setPassword("guest");
        //设置服务器地址
        connectionFactory.setVirtualHost("/");
        //创建与mq的tcp连接
         connection = connectionFactory.newConnection();
         //创建通道
          channel = connection.createChannel();
          //声明交换机 String exchange, BuiltinExchangeType type
            /*
            *参数列表
            * 1.交换机名称
            * 2.交换机类型，fanout、topic、direct、headers
            * */
            channel.exchangeDeclare(EXCHANGE_TOPICS_INFORM, BuiltinExchangeType.TOPIC);
            //声明队列
            // (String queue, boolean durable, boolean exclusive, boolean autoDelete, Map<String,Object> arguments)
                /**
          * 参数明细：
          * 1、队列名称
          * 2、是否持久化
          * 3、是否独占此队列
          * 4、队列不用是否自动删除
          * 5、参数
          */
                //email的消息队列
                channel.queueDeclare(QUEUE_INFORM_EMAIL,true,false,false,null);
                //sms的消息队列
                channel.queueDeclare(QUEUE_INFORM_SMS,true,false,false,null);


                //交换机和队列的绑定 String queue, String exchange, String routingKey
                //绑定email和交换机
                channel.queueBind(QUEUE_INFORM_EMAIL,EXCHANGE_TOPICS_INFORM,"inform.#.email.#");
                //绑定SMS和交换机
                channel.queueBind(QUEUE_INFORM_SMS,EXCHANGE_TOPICS_INFORM,"inform.#.sms.#");


                //编写发送消息

            for (int i = 0; i < 5; i++) {


            String message="你好世界email"+System.currentTimeMillis();

            //发送消息
            /*
            * 参数列表
            * 1.交换机名称（Exchange），如果没有指定，则使用Default Exchange
            * 2.routingKey,消息的路由Key，是用于Exchange（交换机）将消息转发到指定的消息队列
            * 3.消息包含的属性
            * 4.消息体
            * 这里没有指定交换机，消息将发送给默认交换机，每个队列也会绑定那个默认的交换机，但是不能显
              示绑定或解除绑定
              */
            channel.basicPublish(EXCHANGE_TOPICS_INFORM,"inform.email",null,message.getBytes("utf-8"));

            System.out.println("send message is:"+message+",");
            }
            for (int i = 0; i <5 ; i++) {
                String message="你好世界sms"+System.currentTimeMillis();
                channel.basicPublish(EXCHANGE_TOPICS_INFORM,"inform.sms",null,message.getBytes("utf-8"));
                System.out.println("send message is:"+message+",");
            }
            for (int i = 0; i <5 ; i++) {
                String message="你好世界sms_eamil"+System.currentTimeMillis();
                channel.basicPublish(EXCHANGE_TOPICS_INFORM,"inform.sms.email",null,message.getBytes("utf-8"));
                System.out.println("send message is:"+message+",");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                channel.close();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (TimeoutException e) {
                e.printStackTrace();
            }
            try {
                connection.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
